package com.iyimo.control
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class Button{
		/**
		 * button携带的数据
		 */
		private var _data:*
		
		/**
		 * 样式,有4帧
		 */
		protected var _style:MovieClip;
		
		/**
		 * 文字,可以为空
		 */		
		protected var _label:String;
		
		/**
		 * 是否可用
		 */
		protected var _enable:Boolean = true;
		
		public function Button(){}

		/**
		 * 简单button,只有图标和label
		 * @return 
		 */		
		public static function createSimpleBtn(style:MovieClip, label:String):Button{
			var btn:Button = new Button();
			btn.style = style;
			btn.label = label;
			return btn;
		}
		
		public function set label(value:String):void{
			if (value == null)return;
			_label = value;
			if(_style){
				(_style["label"] as TextField).text = _label;
			}
		}

		public function set style(value:MovieClip):void{
			_style = value;
			_style.gotoAndStop("normal");
			
			if (_label){
				(_style["label"] as TextField).text = _label;
			}
			
			addListener();
		}
		
		/**
		 * 是否可用
		 */
		public function get enable():Boolean{
			return _enable;
		}
		
		public function set enable(value:Boolean):void{
			_enable = _style.mouseEnabled = _style.mouseChildren = value;
			
			if (_enable)
				_style.gotoAndStop("normal");
			else
				_style.gotoAndStop("disable");
		}
		
		/**
		 * button携带的数据
		 */
		public function set data(value:*):void{
			_data = value;
			_style.data = value;
		}
		
		public function get data():*{
			return _data;
		}
		
		/**
		 * 添加监听
		 */		
		public function addEventListener(type:String, listener:Function):void{
			_style.addEventListener(type, listener);
		}
		
		/**
		 * remove监听
		 */		
		public function removeEventListener(type:String, listener:Function):void{
			_style.removeEventListener(type, listener);
		}
		
		/**
		 * 鼠标放上去,更改style
		 */		
		protected function onOver(e:MouseEvent):void{
			if (_enable)
				_style.gotoAndStop("over");
		}
		
		/**
		 * 鼠标放上去,更改style
		 */
		protected function onOut(e:MouseEvent):void{
			if (_enable)
				_style.gotoAndStop("normal");
		}
		
		/**
		 * 鼠标按下,更改style
		 */
		protected function onDown(e:MouseEvent):void{
			if (_enable)
				_style.gotoAndStop("down");
		}
		
		/**
		 * 鼠标松开,更改style
		 */
		protected function onUp(e:MouseEvent):void{
			if (_enable)
				_style.gotoAndStop("over");
		}
		
		/**
		 * 添加各种监听器
		 */		
		private function addListener():void{
			_style.addEventListener(MouseEvent.ROLL_OVER, onOver);
			_style.addEventListener(MouseEvent.ROLL_OUT, onOut);
			_style.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_style.addEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		/**
		 * 去处各种监听器
		 */		
		private function removeListener():void{
			_style.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			_style.removeEventListener(MouseEvent.ROLL_OUT, onOut);
			_style.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_style.removeEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		/**
		 * 销毁自身
		 */		
		public function dispose():void{
			removeListener();
			_style = null;
		}
	}
}